<template>
	<view>
		<!-- 收货地址 -->
		<view class="revice_address_row">
			<view class="address_btn" v-if="!address.userName"
			@click="handleChooseAddress" >收货地址 >
			</view>
			<view v-else class="user_info_row">
				<view class="user_info">
					<view>{{address.userName}}</view>
					<view>{{address.all}}</view>
				</view>
				<view class="cp">
				<view class="user_phone">{{address.telNumber}}</view>
				<view class="choose" @click="choose">选择收货地址 ></view>
				</view>
			</view>
		</view>
		<!-- 购物车内容 -->
		<view class="cart_content">
			<view class="iconfont icon-baobei"></view>
			<view class="cart_main">
				<block v-if="cart.length!==0">
				<view class="cart_item"
				v-for="item in cart"
				:key="item.goods_id"
				>
				<!-- 复选框 -->
				<view class="cart_chk_wrap">
					<checkbox-group :data-id="item.goods_id" @change="handleItemChange">
						<checkbox :checked="item.checked"></checkbox>
					</checkbox-group>
				</view>
				<!-- 图片 -->
				<navigator class="cart_img_wrap"
				:url="`/pages/goods_detail/main?goods_id={{item.goods_id}}`"
				>
					<image mode="widthFix" :src="item.goods_small_logo"></image>
				</navigator>
				 <!-- 信息 -->
				<view class="cart_info_wrap">
					<navigator class="goods_name"
					:url="`/pages/goods_detail/main?goods_id={{item.goods_id}}`"
					>{{item.goods_name}}</navigator>
					<view class="goods_price_wrap">
						<navigator class="goods_price"
						:url="`/pages/goods_detail/main?goods_id={{item.goods_id}}`"
						>￥{{item.goods_price}}</navigator>
						<view class="cart_num_tool">
							<view @click="handleItemNumEdit" :data-id="item.goods_id" :data-operation="-1" class="num_edit">-</view>
							<view class="goods_num">{{item.num}}</view>
							<view @click="handleItemNumEdit" :data-id="item.goods_id" :data-operation="1" class="num_edit">+</view>
						</view>
					</view>
				</view>
				</view>
				</block>
				<block v-else>
				<image mode="widthFix" src="@/static/img/gouwuchekong.jpg"></image>
				<navigator class="to-btn" url="/pages/more/index">
				去逛逛 >
				</navigator>
				</block>
			</view>
		</view>

		<!-- 底部工具栏 -->
		<view class="footer_tool">
			<!-- 全选 -->
			<view class="all_chk_wrap">
				<checkbox-group @change="handleItemAllchecked">
					<checkbox :checked="allChecked">全选</checkbox>
				</checkbox-group>
			</view>
			<!-- 总价格 -->
			<view class="total_price_wrap">
				<view class="total_price">
				合计：<text class="total_price_text">￥{{totalPrice}}</text>
				</view>
				<view>包含运费</view>
			</view>
			<!-- 结算 -->
			<view class="order_pay_wrap" @click="handlePay">结算({{totalNum}})</view>
		</view>

	</view>
</template>

<script>
	export default {
		data(){
			return{
				address:{},
				cart:[],
				allChecked:false,
				totalPrice:0,
				totalNum:0
			}
    	},
		onShow(){
			//获取缓存中收货地址信息
			this.address=uni.getStorageSync('address');
			//获取缓存中购物车数据
			const cart=uni.getStorageSync('cart')||[];
			
			this.setCart(cart);
		},
		
		methods:{

				choose(){
					uni.navigateTo({url:'/pages/get_userinfo/index'})
				},
			    //点击获取收货地址事件
				handleChooseAddress(){
					uni.chooseAddress({
					success: (result) => {
						let address = result;
						address.all = address.provinceName+address.cityName+address.countyName+address.detailInfo;
						uni.setStorageSync('address',address);
					}
					});
					// uni.getSetting({
					//   success:(result) => {
					//       //获取权限状态
					//       const scropeAddress = result.authSetting["scope.address"];
					//       if(scropeAddress===true || scropeAddress===undefined){
					//         uni.chooseAddress({
					//           success: (result1) => {
					//               console.log(result1);
					//           }
					//         });
					//       }else{
					//           uni.openSetting({
					//             success:(result2) => {
					//                 uni.chooseAddress({
					//                     success: (result3) => {
					//                         console.log(result3);
					//                     }
					//                   });
					//             }
					//           })
					//       }
					//   }
					// })
				},
				//商品单选
				handleItemChange(e){
					const goods_id =e.currentTarget.dataset.id;
					//获取购物车数组
					let cart = this.cart;
					//找到被勾选商品对象并修改checked
					let index = cart.findIndex(v=>v.goods_id === goods_id);
					cart[index].checked =!cart[index].checked;
					this.setCart(cart);
				},
				//商品全选
				handleItemAllchecked(){
					let cart = this.cart
					let allChecked = this.allChecked;
					allChecked=!allChecked;
					cart.forEach(v=>v.checked = allChecked);
					this.setCart(cart);
				},
				//加减商品数量
				handleItemNumEdit(e){
					const {operation,id} = e.currentTarget.dataset;
					let cart = this.cart;
					const index = cart.findIndex(v=>v.goods_id===id);
					if(cart[index].num===1 &&operation===-1){
						uni.showModal({
							title: '提示',
							content: '您是否要删除？',
							success :(res) =>{
							if (res.confirm) {
								cart.splice(index,1);
								this.setCart(cart);
						
							} else if (res.cancel) {
								console.log('取消成功')
							}
							}
						})
					}else{
						cart[index].num+=operation;
						this.setCart(cart);
					}
				},
				//结算功能
				handlePay(){
					//判断有无收货地址 有无选中商品
					const address = this.address;
					const totalNum = this.totalNum;
					if(!address.userName){
						uni.showToast({title:"您还没有选择收货地址",icon:'none'});
						return;
					}
					if(totalNum===0){
						uni.showToast({title:"您还没有选购商品",icon:'none'});
						return;
					}
					//成功跳转支付页面
					uni.navigateTo({
					url: '/pages/pay/index',
					});
				},
				//封装
				setCart(cart){
					//计算全选
					//const allChecked =cart.length?cart.every(v=>v.checked):false;
					let allChecked = true;
					//计算总价格，总数量
					let totalPrice = 0;
					let totalNum = 0;
					cart.forEach(v => {
						if(v.checked){
							totalPrice +=v.goods_price * v.num;
							totalNum += v.num;
						}else{
							allChecked = false;
						}
					});
					//重新设置data和缓存数据
					this.allChecked = cart.length!=0?allChecked:false;
					this.cart = cart;
					this.totalPrice = totalPrice;
					this.totalNum = totalNum;
					
					uni.setStorageSync('cart', cart);
				}
		}
	}
</script>

<style lang="scss">
	page{
  		padding-bottom: 90rpx;
	}
	.revice_address_row{
		
	.address_btn{
			padding: 20rpx;
			text-align: right ;
			font-size: 30rpx;
			color: var(--themeColor);
		} 
		
	.user_info_row{
			display: flex;
			padding: 20rpx;
			
			.user_info{
					flex: 5;
					
				}
			
			.cp{
				flex: 2;
				text-align: right;
				.user_phone{
					
					
				}
				.choose{
					padding-top: 10rpx;
				
				}
			}
			
		}
	}
	.cart_content {
	.iconfont {
				padding: 20rpx;
				font-size: 40rpx;
				color: var(--themeColor);
				border-top: 1rpx solid #ccc;
				border-bottom: 1rpx solid #ccc;
		}
	
		
	.cart_main {
		
		.cart_item {
			display: flex;
			padding: 10rpx;
			border-bottom: 1rpx solid #ccc;
		.cart_chk_wrap {
				flex:1;
				display: flex;
				justify-content: center;
				align-items: center;
			checkbox-group {
				checkbox {
	
				}
			}
			}
	
			
		.cart_img_wrap {
				flex: 2;
				display: flex;
				justify-content: center;
				align-items: center;
			image {
				width: 80%;
			}
			}
	
			
		.cart_info_wrap {
			flex: 4;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			.goods_name {
					display: -webkit-box;
					overflow: hidden;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2; 
					color: #666;
			}
	
			
			.goods_price_wrap {
				display: flex;
				justify-content: space-between;
			.goods_price {
					color: #e64340;
					font-size: 34rpx;
				}
	
				
			.cart_num_tool {
				display: flex;
				.num_edit {
						width: 55rpx;
						height: 55rpx;
						display: flex;
						justify-content: center;
						align-items: center;
						border: 1rpx solid #ccc;
				}
	
				
				.goods_num {
						width: 55rpx;
						height: 55rpx;
						display: flex;
						justify-content: center;
						align-items: center;
				}
				}
			}
			}
		}


		.to-btn{
			margin-left: 230rpx;
			margin-top: 8rpx;
			padding: 15rpx 0;
			background-color: var(--themeColor);
			color: #fff;
			width: 40%;
			text-align: center;
			border-radius: 10rpx;
			
		}
		
		}
	}

	.footer_tool {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 90rpx;
		background-color: #fff;
		display: flex;
		border-top: 1rpx solid #ccc;
		.all_chk_wrap {
		flex: 2;
		display: flex;
		justify-content: center;
		align-items: center;
		checkbox-group {
			checkbox {
	
			}
		}
		}
	
		.total_price_wrap {
		flex: 5;
		padding-right: 15rpx;
		text-align: right;
		.total_price {
			font-size: 34rpx;
			.total_price_text {
			color: #e64340;
			font-size: 34rpx;
			font-weight: 600;
			}
		}
	
		}
	
		.order_pay_wrap {
		flex: 3;
		background-color: var(--themeColor);
		color: #fff;
		font-weight: 600;
		display: flex;
		justify-content: center;
		align-items: center;
		}
	}
</style>